package com.acegear.horizon.domain.events.handlers;

import com.acegear.horizon.domain.events.ClubClaimEvent;
import com.acegear.horizon.domain.events.ClubCreateEvent;
import com.acegear.horizon.domain.events.ClubDeleteEvent;
import com.acegear.horizon.domain.events.ClubModifiedEvent;
import com.acegear.horizon.domain.models.Club;
import com.acegear.horizon.domain.models.User;
import com.acegear.horizon.domain.models.constraint.ClubMemberType;
import com.acegear.horizon.domain.models.constraint.ContentType;
import com.acegear.horizon.domain.models.jpa.*;
import com.acegear.horizon.domain.repository.ClubRepository;
import com.acegear.horizon.domain.repository.UserRepository;
import com.acegear.horizon.domain.repository.jpa.ClubMemberRepository;
import com.acegear.horizon.vendor.ons.ONSConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Created by wangsike on 2016/12/21.
 */
@Component
@Transactional
public class ClubEventHandler {

    @Autowired
    private ClubRepository clubRepository;

    @Autowired
    private ClubMemberRepository clubMemberRepository;

    @Autowired
    private UserRepository userInfoRepository;

    @Autowired
    private ONSConsumer consumer;

    @PostConstruct
    public void newClubONS() {
        consumer.consume(ClubCreateEvent.class, this::newClub);
    }

    @PostConstruct
    public void updateClubONS() {
        consumer.consume(ClubModifiedEvent.class, this::updateClub);
    }

    @PostConstruct
    public void DeleteClubONS() {
        consumer.consume(ClubDeleteEvent.class, this::DeleteClub);
    }

    @PostConstruct
    public void createClubClaimONS() {
        consumer.consume(ClubClaimEvent.class, this::createClubClaim);
    }

    public void newClub(ClubCreateEvent clubCreateEvent) {

        ClubBase clubBase = new ClubBase(clubCreateEvent.getClubId(), clubCreateEvent.getName(), clubCreateEvent.getImgUrl(), clubCreateEvent.getCoverUrl(), clubCreateEvent.getOwner());

        ClubInfo clubInfo = new ClubInfo();
        clubInfo.setClubId(clubBase.getClubId());
        clubInfo.setContact(new ContactInfo(null, clubCreateEvent.getPhone(), null));
        clubInfo.setAddress(new Address(null, null, null, null, clubCreateEvent.getAddress()));
        List<ContentPart> contentParts = new ArrayList<>();
        contentParts.add(new ContentPart(ContentType.TEXT, clubCreateEvent.getMemo(), 1));
        clubInfo.setIntroduction(contentParts);
        clubInfo.setInfo(clubCreateEvent.getMemo());
        Club club = new Club(clubBase, clubInfo);

        club.save();

        if (club.getBase().getRegister()) {
            if (clubCreateEvent.getUserId() != null) {
                Optional<User> userOptional = userInfoRepository.findByUserId(clubCreateEvent.getUserId());
                if (userOptional.isPresent()) {
                    club.addAndSaveMember(userOptional.get().getUserInfo(), ClubMemberType.FOUNDER);
                }
            }
        }
    }

    @Transactional
    public void updateClub(ClubModifiedEvent clubModifiedEvent) {

        Optional<Club> club = clubRepository.findByClubId(clubModifiedEvent.getClubId());
        if (club.isPresent()) {
            ClubBase clubBase = club.get().getBase();
            ClubInfo clubInfo = club.get().getInfo();

            clubBase.setClubName(clubModifiedEvent.getName());
            clubBase.setBackground(clubModifiedEvent.getCoverUrl());
            clubBase.setLogo(clubModifiedEvent.getImgUrl());
            clubBase.setRegister(clubModifiedEvent.getOwner());
            clubInfo.getAddress().update(new Address(null, null, null, null, clubModifiedEvent.getAddress()));
            clubInfo.getContact().update(new ContactInfo(null, clubModifiedEvent.getPhone(), null));

            List<ContentPart> contentParts = new ArrayList<>();
            contentParts.add(new ContentPart(ContentType.TEXT, clubModifiedEvent.getMemo(), 1));
            clubInfo.setIntroduction(contentParts);
            clubInfo.setInfo(clubModifiedEvent.getMemo());
            club.get().save();

            //判断是否有主理人,存入clubMember
            if (club.get().getBase().getRegister()) {
                if (clubModifiedEvent.getUserId() != null) {
                    List<ClubMember> clubMemberList = clubMemberRepository.findByClubIdAndRank(clubModifiedEvent.getClubId(), ClubMemberType.FOUNDER);
                    if (clubMemberList.isEmpty()) {
                        Optional<User> userOptional = userInfoRepository.findByUserId(clubModifiedEvent.getUserId());
                        if (userOptional.isPresent()) {
                            club.get().addAndSaveMember(userOptional.get().getUserInfo(), ClubMemberType.FOUNDER);
                        }
                    }
                }
            }
        }
    }

    @Transactional
    public void DeleteClub(ClubDeleteEvent clubDeleteEvent) {
        Optional<Club> clubOptional = clubRepository.findByClubId(clubDeleteEvent.getClubId());
        if (clubOptional.isPresent()) {
            Club club = clubOptional.get();
            club.delete();
        }
    }

    public void createClubClaim(ClubClaimEvent clubClaimEvent) {
    }
}
